<template>
	<div class="app-container">
		<div style="margin-bottom: 20px;display: flex;justify-content: space-between;align-items: center;">
			<el-radio-group v-model="queryParams.listStatus" @change="getList">
				<el-radio-button label="1">全部订单</el-radio-button>
				<el-radio-button label="2">未接单</el-radio-button>
				<el-radio-button label="3">已接单</el-radio-button>
				<el-radio-button label="4">取消预约</el-radio-button>
			</el-radio-group>
			<!-- 滚动区 -->
			<div style="width: 600px;" v-if="noticeCon">
				<notice-bar :speed="20" v-if="noticeCon" :text="noticeCon" @click="openTip" />
			</div>
		</div>
		<!-- <component :is="radio1" ref="radio1"></component> -->
		<div class="">
			<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
				label-width="80px">
				<div class="app-space-between">
					<div class="app-space-between-left">
						<el-form-item label="订单编号" prop="orderId">
							<el-input v-model="queryParams.orderId" placeholder="请输入订单编号" clearable @clear="handleQuery"
								@keyup.enter.native="handleQuery" style="width: 230px;" />
						</el-form-item>
						<el-form-item label="订单时间" prop="time">
							<el-date-picker v-model="queryParams.time" type="datetimerange" start-placeholder="开始日期"
								end-placeholder="结束日期" @change="handleQuery" :default-time="['12:00:00']"
								value-format="yyyy-MM-dd HH:mm:ss">
							</el-date-picker>
						</el-form-item>
						<el-form-item label="房客电话" prop="arrivePhone">
							<el-input v-model="queryParams.arrivePhone" placeholder="请输入房客电话" clearable
								@clear="handleQuery" @keyup.enter.native="handleQuery" style="width: 230px;" />
						</el-form-item>
						<el-form-item label="房型" prop="goodsName">
							<el-input v-model="queryParams.goodsName" placeholder="请输入房型" clearable @clear="handleQuery"
								@keyup.enter.native="handleQuery" style="width: 230px;" />
						</el-form-item>
					</div>
					<div class="app-space-between-right">
						<el-form-item>
							<el-button type="primary" size="mini" @click="handleQuery">搜索</el-button>
							<el-button size="mini" @click="resetQuery">重置</el-button>
						</el-form-item>
					</div>
				</div>
			</el-form>

			<el-row :gutter="10" class="mb8">
				<el-col :span="1.5" v-if="queryParams.listStatus == '1'||queryParams.listStatus=='3'">
					<el-button type="primary" plain size="mini" @click="handleExport">导出</el-button>
				</el-col>
				<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
			</el-row>

			<el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
				<el-table-column label="订单编号" align="center" prop="orderId" min-width="200">
					<template slot-scope="scope">
						<div style="cursor: pointer;" @click="handleDetails(scope.row)">{{scope.row.orderId}}</div>
					</template>
				</el-table-column>
				<el-table-column label="房客姓名" align="center" prop="userName" min-width="120">
					<template slot-scope="scope">
						<div>{{scope.row.userName}}</div>
					</template>
				</el-table-column>
				<el-table-column label="房客电话" align="center" prop="arrivePhone" min-width="150">
					<template slot-scope="scope">
						<div>{{scope.row.arrivePhone}}</div>
					</template>
				</el-table-column>

				<el-table-column label="房型" align="center" prop="goodsName" min-width="140"></el-table-column>
				
				<el-table-column label="预订房间数量" align="center" prop="goodsName" min-width="140">
					<template slot-scope="scope">
						<div>{{scope.row.userName | getRoomNumb }}</div>
					</template>
				</el-table-column>

				<el-table-column label="入住离店日期" align="center" prop="inAndOutTime" min-width="240">
					<template slot-scope="scope">
						<div>{{scope.row.inAndOutTime}}</div>
					</template>
				</el-table-column>

				<el-table-column label="订单金额" align="center" prop="totalPrice" min-width="80">
					<template slot-scope="scope">
						<div>{{scope.row.totalPrice}}</div>
					</template>
				</el-table-column>
				<el-table-column label="订单状态" align="center" prop="orderStatueName" min-width="120">
					<template slot-scope="scope">
						<div>{{scope.row.orderStatueName}}</div>
					</template>
				</el-table-column>

				<el-table-column label="操作" align="center" min-width="120" fixed="right">
					<template slot-scope='scope'>
						<div v-if="scope.row.orderStatus == '13'">
							<el-button size="mini" type="text" @click="handleCheckIn(scope.row,true)">接单</el-button>
							<el-button size="mini" type="text" @click="handleCancel(scope.row,1)">取消预约</el-button>
						</div>
						<div v-if="scope.row.orderStatus == '99' || scope.row.orderStatus == '34'">
							<el-button size="mini" type="text" @click="handleCancel(scope.row,2)">取消订单</el-button>
						</div>
					</template>
				</el-table-column>
			</el-table>

			<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
				:limit.sync="queryParams.pageSize" @pagination="getList" />
		</div>

		<!-- 弹框 -->
		<el-dialog title="订单详情" :visible.sync="open" width="900px" :close-on-click-modal="false">
			<div v-loading="openContentLoading">
				<el-form ref="ruleForm" label-width="140px">
					<el-row>
						<el-col :span="9">
							<el-form-item label="订单编号：">{{options.orderNumber}}</el-form-item>
						</el-col>
						<el-col :span="6">
							<el-form-item label="订单状态：">{{options.orderStatus}}</el-form-item>
						</el-col>
						<el-col :span="9">
							<el-form-item label="预约时间：">{{options.createTime | $YYYY_MM_DD_hh_mm_ss}}</el-form-item>
						</el-col>
					</el-row>

					<el-row v-if="options.orderStatus && options.orderStatus.indexOf('取消') != -1">
						<el-col :span="24">
							<el-form-item :label="options.orderStatus+'原因：'">{{options.reason}}</el-form-item>
						</el-col>
					</el-row>

					<el-row>
						<el-col :span="24">
							<el-form-item label="预约房型：">{{options.goodsName}}</el-form-item>
						</el-col>
					</el-row>

					<el-row>
						<el-col :span="24">
							<el-form-item label="房客姓名：">{{options.arriveName}}</el-form-item>
						</el-col>
					</el-row>

					<el-row>
						<el-col :span="24">
							<el-form-item label="房客电话：">{{options.arrivePhone}}</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="12">
							<el-form-item label="住宿时间：">{{options.inTime | $YYYY_MM_DD}} 至
								{{options.outTime | $YYYY_MM_DD}}
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="住宿天数：">{{options.dayNum}}天</el-form-item>
						</el-col>
					</el-row>
					
					<el-row>
						<el-col :span="24">
							<el-form-item label="特殊要求：">{{options.specialRequest}}</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="24">
							<el-form-item label="房价：">
								<div class="room-table">
									<div class="room-table-price">实付金额：￥{{options.totalPrice}}</div>
									<div class="room-table-box">
										<div class="room-table-box-item" v-for="(item,index) in options.priceDetail"
											:key="index" :style="options.priceDetail.length == 1? 'border-right: none;' : ''">
											<div class="cell-1">{{item.times}}</div>
											<div class="cell-2">房间单价：￥{{{price:item.price,arriveName:options.arriveName}|getUnitPrice}}</div>
											<div class="cell-3">房间数量：{{options.arriveName|getRoomNumb}}间</div>
										</div>
									</div>
								</div>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</div>

			<span slot="footer" class="dialog-footer" v-if="!openContentLoading && options._status == '13'">
				<el-button type="primary" @click="handleCheckIn(operateOptions)">接 单</el-button>
				<el-button @click="handleCancel(operateOptions,1)" style="margin-left: 60px;">取消预约</el-button>
			</span>
		</el-dialog>

		<el-dialog :title="showCancelReservationState == 1?'取消预约':'取消订单'" :visible.sync="showCancelReservation"
			width="600px" :close-on-click-modal="false">
			<el-form label-width="110px">
				<el-form-item label="操作人:">
					{{$store.state.user.name}}
				</el-form-item>
				<el-form-item :label="showCancelReservationState == 1?'取消预约原因:':'取消订单原因:'">
					<el-input type="textarea" placeholder="请输入内容" rows="10" v-model="cancelReservationVal"
						maxlength="2000" show-word-limit />
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="showCancelReservation = false" style="margin-right: 40px;">取 消</el-button>
				<el-button type="primary" :loading="showCancelReservationBtnLoading"
					@click="cancelOperate(operateOptions,showCancelReservationState)">确 定</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	import comAll from '../components/Tabs/all';
	import comHanginTheAir from '../components/Tabs/hanginTheAir';
	import comCompleted from '../components/Tabs/completed';
	import comDeleted from '../components/Tabs/deleted';
	import noticeBar from "@/components/NoticeBar";
	import {
		goodsWarningList
	} from "@/api/hotel/content";
	import {
		orderManage
	} from "@/api/order/content";
	import {
		getConfigKey
	} from "@/api/system/config.js";

	import {
		hotelOrderList,
		hotelOrderCheckIn,
		cancelReservation,
		cancelOrder
	} from "@/api/hotel/order";
	export default {
		components: {
			comAll,
			comHanginTheAir,
			comCompleted,
			comDeleted,
			noticeBar,
		},
		data() {
			return {
				radio1: 'comAll',
				noticeCon: '',
				time: null,
				open: false,
				openContentLoading: false,
				showCancelReservationBtnLoading: false,
				showCancelReservationState: 1,
				showCancelReservation: false,
				cancelReservationVal: '',
				options: {},
				operateOptions: {},
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 岗位表格数据
				postList: [],
				// 弹出层标题
				title: "",
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					status: undefined,
					orderId: undefined,
					goodsName: undefined,
					userName: undefined,
					time: undefined,
					arrivePhone: undefined,
					listStatus: "1"
				},
			}
		},
		async created() {
			if (this.$route.query.id) this.radio1 = "comHanginTheAir";
			return this.getList();
			let res1 = await goodsWarningList();
			this.noticeCon = res1.msg;
			let res2 = await getConfigKey("sys.refresh"); //刷新时间
		},
		methods: {
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			resetQuery() {
				this.resetForm("queryForm");
				this.queryParams.listStatus = "1";
				this.handleQuery();
			},
			getList() {
				this.loading = true;
				let queryParams = JSON.parse(JSON.stringify(this.queryParams));
				if (queryParams.time) {
					queryParams.orderStartTime = queryParams.time[0]
					queryParams.orderEndTime = queryParams.time[1]
				}
				delete queryParams.time;
				hotelOrderList(queryParams).then(response => {
					this.postList = response.rows.map(item => {
						if (Number(item.orderStatus) != 13) return item;
						let date = new Date(response.rows[0].createTime);
						date.setMinutes(date.getMinutes() + this.minute);
						item.isNewOrder = false;
						if (date.getTime() > Date.now()) item.isNewOrder = true;
						return item;
					});
					this.total = response.total;
					this.loading = false;
				});
			},
			/** 导出按钮操作 */
			handleExport(data) {
				this.download('hotel/order/export', {
					...this.queryParams
				}, `${new Date().getTime()}.xlsx`)
			},
			async handleDetails(options) {
				try {
					this.open = true;
					this.openContentLoading = true;
					this.operateOptions = options;
					this.options = {};
					let res = await orderManage(options.storeOrderId);
					res.data._status = options.orderStatus;
					this.options = res.data;
					this.options.storeOrderId = opeions.storeOrderId
					this.openContentLoading = false;
				} catch (e) {
					this.openContentLoading = false;
				}
			},
			async handleCancelReservation(options) { //取消预约
				try {
					this.showCancelReservationBtnLoading = true;
					let res = await cancelReservation({
						storeOrderId: options.storeOrderId,
						f2: this.cancelReservationVal
					});
					this.getList();
					this.showCancelReservation = false;
					this.showCancelReservationBtnLoading = false;
					this.open = false;
					this.$message.success("操作成功");
				} catch (e) {
					this.showCancelReservationBtnLoading = false;
					console.log(e)
				}
			},
			async handleCancelOrder(options) { //取消订单
				try {
					this.showCancelReservationBtnLoading = true;
					let res = await cancelOrder({
						...options,
						f2: this.cancelReservationVal
					});
					this.getList();
					this.showCancelReservation = false;
					this.showCancelReservationBtnLoading = false;
					this.open = false;
					this.$message.success("操作成功");
				} catch (e) {
					this.showCancelReservationBtnLoading = false;
					console.log(e)
				}
			},
			async handleCancel(options, state) {
				this.operateOptions = options;
				this.showCancelReservationState = state;
				this.cancelReservationVal = '';
				this.showCancelReservation = true;
				this.showCancelReservationBtnLoading = false;
			},
			async cancelOperate(options, state) {
					this.handleCancelOrder(options)
					return
				// if (state == 1) this.handleCancelReservation(options)
				// if (state == 2) {}
			},
			async handleCheckIn(options, open) { //入驻 接单
				try {
					if (open) {
						await this.$confirm('请确认已在酒店系统预约成功房间，再执行接单操作', '提示', {
							confirmButtonText: '确定',
							cancelButtonText: '取消',
							type: 'warning'
						});
					}
					let res = await hotelOrderCheckIn({
						storeOrderId: options.storeOrderId //酒店订单id
					});
					this.getList();
					this.open = false;
					this.showCancelReservation = false;
					this.$message.success("操作成功");

				} catch (e) {
					//TODO handle the exception
					console.log(e)
				}
			},
			openTip(e) {
				console.log("内容：", e)
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.postId)
			},
		},
		filters: {
			getUnitPrice(options){
				if(!options.arriveName) return;
				let numb = options.arriveName.split(',').length;
				return options.price / numb
			},
			getRoomNumb(str){
				if(!str) return;
				return str.split(',').length;
			}
		}
	}
</script>
<style lang="scss" scoped>
	$borderColor: #cccccc;

	::v-deep .el-dialog__footer {
		text-align: center;
	}

	.room-table {
		display: inline-block;
		border: 1px solid $borderColor;
		max-width: 620px;

		.room-table-price {
			width: 100%;
			box-sizing: border-box;
			padding: 0 10px;
			border-bottom: 1px solid $borderColor;
		}

		.room-table-box {
			width: 100%;
			display: flex;
			flex-wrap: wrap;
			margin-left: 1px;
			margin-right: -1px;
			box-sizing: border-box;
			.room-table-box-item {
				width: 154px;
				padding: 10px;
				box-sizing: border-box;
				border-bottom: 1px solid $borderColor;
				border-right: 1px solid $borderColor;
				margin-bottom: -1px;
				
				&:nth-child(4n){
					border-right: none;
				}

				.cell-1 {}

				.cell-2 {
					padding: 10px 0;
				}

				.cell-3 {}
			}
		}
	}

	.wrappar {
		width: 300px;
		border: 1px solid red;
		overflow: hidden;

		.content {}
	}
</style>
